#!/bin/sh
#desc:Organize ports
#type:local
#package:odhal

CTRLPATH="/var/opendomo/control"
CFGPATH="/etc/opendomo/control"

BYZONEPATH="/var/opendomo/control_byzone"
BYTAGPATH="/var/opendomo/control_bytag"

rm -fr $BYZONEPATH
rm -fr $BYTAGPATH

mkdir $BYZONEPATH
mkdir $BYTAGPATH

cd /etc/opendomo/zones/
ZONES=`ls`

cd /etc/opendomo/tags/
TAGS=`ls`

for z in $ZONES; do
	mkdir $BYZONEPATH/$z
done

for t in $TAGS; do
	mkdir $BYTAGPATH/$t
done

cd $CFGPATH
for d in *; do
	if test "$d" != "*" && test -d "$d"; then
		cd $d
		
		for p in *.info; do
			if test $p != "*.info"; then
				pname=`echo $p | cut -f1 -d.`
				echo " Including $CFGPATH/$d/$p"
				zone=""
				tags=""
				. $CFGPATH/$d/$p
				if test "$status" != "disabled"; then
					if ! test -z "$zone"; then
						echo "$d" > $BYZONEPATH/$zone/${d}_${pname}.device
						ln -s $CFGPATH/$d/$pname.info $BYZONEPATH/$zone/${d}_${pname}.info
						ln -s $CFGPATH/$d/$pname.desc $BYZONEPATH/$zone/${d}_${pname}.desc
						ln -s $CTRLPATH/$d/$pname $BYZONEPATH/$zone/${d}_${pname}
						ln -s $CTRLPATH/$d/$pname.value $BYZONEPATH/$zone/$pname.value
					fi
				
					if ! test -z "$tags"; then
						ln -s $CFGPATH/$d/$pname.info $BYTAGPATH/$tags/${d}_${pname}.info
						ln -s $CFGPATH/$d/$pname.desc $BYTAGPATH/$tags/${d}_${pname}.desc
						ln -s $CTRLPATH/$d/$pname $BYTAGPATH/$tags/${d}_${pname}
						ln -s $CTRLPATH/$d/$pname.value $BYTAGPATH/$tags/${d}_${pname}.value	
					fi
				else
					echo " Port $p disabled"
				fi
				
			fi
		done
		
		cd ..
	fi
done
